# -*- coding:utf-8 -*-
from PySide6.QtCore import QDir, QObject, Signal
from app.common.global_config import GlobalConfig

class AppModel(QObject):
    """
    mvc model 实现
    """
    # 新增信号
    # excel文件目录变化信号 
    excel_dir_changed = Signal(str)
    # 导出文件保存目录变化信号 
    export_dir_changed = Signal(str)
    # 文件分割符目录变化信号 
    delimiter_changed = Signal(str)
    # 导出类型变化信号
    export_type_changed = Signal(str)
    # 日志变化信号
    logging_changed = Signal(str) 

    def __init__(self):
        super().__init__()
        self._excel_dir = ""
        self._export_dir = ""
        self._delimiter = "_"
        self._export_type = ""
        self._log_content = ""

    def excel_dir(self):
        return self._excel_dir

    def excel_dir(self, value):
        if self._excel_dir != value:
            self._excel_dir = value
            self.excel_dir_changed.emit(value)
            self.add_log(f"Excel目录更改为：{value}")
            global_config = GlobalConfig.get_instance()
            global_config.set(global_config.EXCEL_DIR_KEY, self._excel_dir)

    def export_dir(self):
        return self._export_dir

    def export_dir(self, value):
        if self._export_dir != value:
            self._export_dir = value
            self.export_dir_changed.emit(value)
            self.add_log(f"导出文件保存目录更改为：{value}") 
            global_config = GlobalConfig.get_instance()
            global_config.set(global_config.EXPORT_DIR_KEY, self._export_dir)

    def delimiter(self):
        return self._delimiter
    
    def delimiter(self, value):
        if self._delimiter != value:
            self._delimiter = value
            self.delimiter_changed.emit(value)
            self.add_log(f"Excel文件中英文名称分隔符改为：{value}")
            global_config = GlobalConfig.get_instance()
            global_config.set(global_config.DELIMITER_KEY, self._delimiter)

    def export_type(self):
        return self._export_type
    
    def export_type(self, value):
        self._export_type = value
        self.logging_changed.emit(value)
        self.add_log(f"Excel文件导出类型：{value}")
        global_config = GlobalConfig.get_instance()
        global_config.set(global_config.EXPORT_TYPE_KEY, self._export_type)

    @property
    def log_content(self):
        return self._log_content

    @log_content.setter
    def log_content(self, value):
        self._log_content = value
        self.logging_changed.emit(value)


    def init_model(self, data):
        """
        去读配置表信息,初始化model信息
        """
        global_config = GlobalConfig.get_instance()
        if global_config.EXCEL_DIR_KEY in data:
            excel_dir = data[global_config.EXCEL_DIR_KEY]
            self.excel_dir(excel_dir)
        if global_config.EXPORT_DIR_KEY in data:
            export_dir = data[global_config.EXPORT_DIR_KEY]
            self.export_dir(export_dir)
        if global_config.DELIMITER_KEY in data:
            delimiter = data[global_config.DELIMITER_KEY]
            self.delimiter(delimiter)
        if global_config.EXPORT_TYPE_KEY in data:
            export_type = data[global_config.EXPORT_TYPE_KEY]
            self.export_type(export_type)

    def add_log(self, message):
        """新增日志记录方法"""
        self.log_content = f"[{QDir.currentPath()}] {message}\n{self.log_content}"